/************************************************************************
Purpose: Make elasticity coefficients plot in the ETS paper
*************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_CLEAN/panel_plant-period-date-bid.dta", clear
drop if bid_status == "REJ"
drop if missing(bid_qty)
sort gpcb_id period date bid_id 
tempfile data_reg
save `data_reg', replace

********************************************************************************
*** Coefficients plot for Plant x Period FEs by Week
********************************************************************************

clear
set obs 6
gen model = .
gen coef = .
gen lower = .
gen upper = .
gen nobs = .

local price_var ln_bid_price
local holding_var ln_permit_holding_hyp

forvalue j = 0/5 {
	
	preserve
	
	use `data_reg', clear
		
	if `j' == 5 {
		reghdfe `price_var' `holding_var' if week_left >= `j', a(plant_period_id) vce(cluster gpcb_id)
	} 
	else {
		reghdfe `price_var' `holding_var' if week_left == `j', a(plant_period_id) vce(cluster gpcb_id)
	}
	
	restore 
	
	replace model = `j' if _n == `j'+1
	replace coef = _b[`holding_var'] if _n == `j'+1
	replace lower = _b[`holding_var'] - invttail(e(df_r),0.025)*_se[`holding_var'] if _n == `j'+1
	replace upper = _b[`holding_var'] + invttail(e(df_r),0.025)*_se[`holding_var'] if _n == `j'+1
	replace nobs = e(N) if _n == `j'+1
	
}

gen model_plot = -model
label define model_plot_values -5 "≥5" -4 "4" -3 "3" -2 "2" -1 "1" 0 "0"
label values model_plot model_plot_values  
graph twoway ///
	(scatter coef model_plot, mcolor("midblue")) (rcap lower upper model_plot, color("midblue")), ///
	yline(0, lstyle(solid) lcolor(red)) xlabel(-5 "≥5" -4 "4" -3 "3" -2 "2" -1 "1" 0 "0") ///
	ytitle("Coefficient of log(emissions as bid)") ///
	xtitle("Weeks remaining") /// 
	plotregion(color(white)) bgcolor(white) graphregion(color(white) lcolor(white)) legend(off) ///
	name("graph_1", replace)
	
********************************************************************************
*** Histogram of number of bids by Week
********************************************************************************
	
use `data_reg', clear
gen model_plot = week_left
replace model_plot = 5 if model_plot > 5
replace model_plot = -model_plot
hist model_plot, freq discrete width(1) ///
	xlabel(-5 "≥5" -4 "4" -3 "3" -2 "2" -1 "1" 0 "0") ///
	color(midblue*0.6) lcolor(midblue) ///
	xtitle("Weeks remaining") ///
	plotregion(color(white)) bgcolor(white) graphregion(color(white) lcolor(white)) legend(off) ///
	name("graph_2", replace) fysize(50)
	
********************************************************************************
*** Combine coefficients plot and histogram
********************************************************************************
	
graph combine graph_1 graph_2, ///
	cols(1) ysize(20) xsize(16) xcommon ///
	graphregion(color(white)) plotregion(color(white) lcolor(white))
	
graph export "$TRADING_FIGS/Figure_F1.pdf", replace
